﻿#ifndef CVT_QT_H
#define CVT_QT_H


#include "basedata.h"
#include "robotdata.h"
#include <QImage>

namespace xvs {




inline QImage toQImage(Image& inImage)
{
//    if (inImage.Depth()->Value() != 1 && inImage.Depth()->Value() != 3)
//    {
//        qDebug() <<"Unsupported image depth";
//        return QImage();
//    }

    if (inImage.Depth()->Value() == 1)
    {
        return QImage(inImage.ConstBits(),inImage.Width()->Value(),inImage.Height()->Value(),inImage.Pitch()->Value(),QImage::Format_Indexed8).copy();
    }
    else if (inImage.Depth()->Value() == 3)
    {
        return QImage(inImage.ConstBits(),inImage.Width()->Value(),inImage.Height()->Value(),inImage.Pitch()->Value(),QImage::Format_RGB888).copy();
    }
    else if (inImage.Depth()->Value() == 4)
    {
        return QImage(inImage.ConstBits(),inImage.Width()->Value(),inImage.Height()->Value(),inImage.Pitch()->Value(),QImage::Format_RGB32).copy();
    }
    qDebug() <<"Unsupported image depth";
    return QImage();


//    const QImage::Format format = inImage.Depth()->Value() == 3 ? QImage::Format_RGB888 : QImage::Format_Grayscale8;
//    std::shared_ptr<QImage> imagePtr = std::make_shared<QImage>(inImage.Width()->Value(), inImage.Height()->Value(), format);
//    int size = inImage.Width()->Value() * inImage.Height()->Value() * inImage.Depth()->Value();

//    memcpy(imagePtr->bits(),inImage.ConstBits(),size);
//    return *imagePtr.get();

//    return QImage();
}

inline void fromQImage(QImage& inImage,Image& outImage)
{
//    bool needConvert = (inImage.format() != QImage::Format_RGB888) && (inImage.format() != QImage::Format_ARGB32);
//    QImage image = needConvert ? inImage.convertToFormat(QImage::Format_RGB888) : inImage;
//    int depth = image.format() == QImage::Format_RGB888 ? 3 : 4;

    PlainType type;//默认UInt8 Real/Int等其他类型待验证
    outImage = Image(inImage.width(), inImage.height(), type, inImage.depth()/8,Image::Pitch(type,inImage.depth()/8,inImage.width(),4), inImage.bits());
}

inline QVector<QLineF> toQVectorLineF(Region& inRegion)
{
    if (inRegion.IsNull().Value())
        return QVector<QLineF>();

    QVector<QLineF> vectLine;
    for (int i = 0; i< inRegion.ArrayPointRun()->Count(); i++)
    {
        PointRun pointRun = inRegion.ArrayPointRun()->At<PointRun>(i);
        vectLine.push_back(QLineF(pointRun.X()->Value(),pointRun.Y()->Value(),pointRun.X()->Value()+pointRun.Length()->Value(),pointRun.Y()->Value()));
    }
    return vectLine;
}
//从QVector<QLineF> 结构体中获取Region
inline void fromQVectorLineF(QVector<QLineF>& inVectLine,int frameWidth,int frameHeight,Region& outRegion)
{
    outRegion.Clear();
    if (inVectLine.size() <= 0 || frameWidth <= 0 || frameHeight <= 0) return;

    Integer _width(frameWidth);
    Integer _height(frameHeight);
    outRegion.SetFrameWidth(&_width);
    outRegion.SetFrameHeight(&_height);
    for (int i = 0; i< inVectLine.size(); i++)
    {
        QLineF line = inVectLine.at(i);
        PointRun pointRun(Integer((int)line.x1()),Integer((int)line.y1()),Integer((int)(line.x2() - line.x1())));
        outRegion.ArrayPointRun()->Push_Back(pointRun);
    }
}

//inline QRectF toQRectF(Rectangle2D& rect)
//{
//    return QRectF(rect.Origin()->X(),rect.Origin()->Y(),rect.Width(),rect.Height());
//}
//knq

inline QRectF toQRectF(Rectangle2DInt& rect)
{
    return QRectF(rect.Origin()->X()->Value(),rect.Origin()->Y()->Value(),rect.Width()->Value(),rect.Height()->Value());
}


}

#endif // CVT_QT_H
